const { read, write } = require('../lib/read&write')

const ACCOUNTSURL = '../data/accounts.json'
function setAccount(account) {
  const __Account = read(ACCOUNTSURL)
  __Account.push(account)
  write(__Account, ACCOUNTSURL)
}

function editAccount(account) {
  const __Account = read(ACCOUNTSURL)
  __Account.splice(
    __Account.findIndex((item) => item.id == account.id),
    1,
    account,
  )
  write(__Account, ACCOUNTSURL)
}

function deleteAccount(accountId) {
  const __Account = read(ACCOUNTSURL)
  let targetAccount = searchAccount(accountId)
  __Account.splice(
    __Account.findIndex((item) => item.id == targetAccount.id),
    1
  )
  write(__Account, ACCOUNTSURL)
}

function getAllAccounts() {
  const __Account = read(ACCOUNTSURL)
  return __Account
}

function searchAccount(account) {
  const __Account = read(ACCOUNTSURL)
  return __Account.find((item) => item.account === account)
}

function searchAccountById(userId) {
  const __Account = read(ACCOUNTSURL)
  return __Account.find((item) => item.id === userId)
}

module.exports = {
  searchAccount,
  getAllAccounts,
  editAccount,
  setAccount,
  searchAccountById,
  // 以下未用
  deleteAccount,
}
